import datetime
import backtrader as bt

#简单移动平均
class SimpleMA(bt.Strategy):
    def __init__(self):
        self.sma = bt.indicators.SimpleMovingAverage(self.data, period=20, plotname="20 SMA")

# 初始化Cerebro引擎, 禁用数据监测
cerebro = bt.Cerebro(stdstats=False)

# 设置日期参数并添加至Cerebro引擎
data1 = bt.feeds.YahooFinanceCSVData(
	dataname='TSLA.csv',
	fromdate=datetime.datetime(2018, 1, 1),
	todate=datetime.datetime(2020, 10, 5))

cerebro.adddata(data1)

#second data feed
# data2 = bt.feeds.YahooFinanceCSVData(
# 	dataname='AAPL.csv',
# 	fromdate=datetime.datetime(2018, 1, 1),
# 	todate=datetime.datetime(2020, 1, 1))
#
# data2.compensate(data1)
# data2.plotinfo.plotmaster = data1
# data2.plotinfo.sameaxis = True

# cerebro.adddata(data2)
#

#向图上添加均线指标
cerebro.addstrategy(SimpleMA)

# 运行Cerebro引擎
cerebro.run()
cerebro.plot()


'''
REFERENCE: 画图参数
plotinfo = dict(plot=True,
                subplot=True,
                plotname='',
                plotskip=False,
                plotabove=False,
                plotlinelabels=False,
                plotlinevalues=True,
                plotvaluetags=True,
                plotymargin=0.0,
                plotyhlines=[],
                plotyticks=[],
                plothlines=[],
                plotforce=False,
                plotmaster=None,
                plotylimited=True,
           )
'''